package org.xussen.dic;

import java.io.BufferedReader;
import java.io.IOException;

public class InitDictionary {

	/**
	 * base: 数组用来存放单词的转换..其实就是一个DFA转换过程
	 */
	public static int[] base;
	/**
	 * check: 数组用来验证里面存储的是上一个状态的位置
	 */
	public static int[] check;
	/**
	 * status: 用来判断一个单词的状态 1.为不成词.处于过度阶段 2.成次也可能是词语的一部分. 3.词语结束 example: 中 1 中华
	 * 2 中华人 1 中华人民 3
	 */
	public static byte[] status;
	
	private static String path = "data/arrays.dic";

	static {
		init();
	}

	private static void init() {
		long start = System.currentTimeMillis();
		try {
			initArrays();
			System.out.println("init core library ok use time :" + (System.currentTimeMillis() - start) );
		} catch (Exception e) {
			e.printStackTrace();
			System.err.print("init library core library error! ");
		}
	}

	/**
	 * 对于base,check,natrue,status的加载 0.代表这个字不在词典中 1.继续 2.是个词但是还可以继续 3.停止已经是个词了
	 * 
	 * @throws Exception
	 */
	public static void initArrays() throws Exception {
		BufferedReader reader = IOUtil.getReader(path, "utf-8");
		initArraySize(reader);
		reader.close();
		reader = IOUtil.getReader(path, "utf-8");
		initArrays(reader);
		reader.close();
	}

	private static void initArraySize(BufferedReader reader) throws IOException {
		// TODO Auto-generated method stub
		String temp = null;
		String last = null;
		while ((temp = reader.readLine()) != null) {
			last = temp;
		}

		String[] strs = last.split(" ");

		int arrayLength = Integer.parseInt(strs[0]) + 1;

		base = new int[arrayLength];

		check = new int[arrayLength];

		status = new byte[arrayLength];

	}

	public static void initArrays(BufferedReader reader) throws Exception {
		/**
		 * 下面开始加载词典
		 */
		String temp = null;
		String[] strs = null;
		int num;
		while ((temp = reader.readLine()) != null) {
			strs = temp.split(" ");
			num = Integer.parseInt(strs[0]);
//			base[num] = Integer.parseInt(strs[1]);
//			check[num] = Integer.parseInt(strs[2]);
//			status[num] = Byte.parseByte(strs[3]);
			base[num] = Integer.parseInt(strs[2]);
			check[num] = Integer.parseInt(strs[3]);
			status[num] = Byte.parseByte(strs[4]);
		}
	}
	/**
	 * 
		if (StringUtil.isBlank(str)) {
			return true;
		}
		int baseValue = str.charAt(0);
		int checkValue = 0;
		for (int i = 1; i < str.length(); i++) {
			checkValue = baseValue;
			baseValue = base[baseValue] + str.charAt(i);
			if (baseValue > check.length - 1)
				return false;
			if (check[baseValue] != -1 && check[baseValue] != checkValue) {
				return false;
			}
		}
		return status[baseValue] > 1;
	 * @param content
	 * @return
	 */
	public static String getPosition(String content) {
		for (int j = 0; j < content.length(); j++) {
			int pre = 0;
			int c = content.charAt(j);
			for (int i = j+1; i < content.length(); i++) {
				pre = c;
				c = base[c] + content.charAt(i);
				if(c > check.length-1) {
					break;
				}
				if(check[c] != -1 && check[c] != pre) {
					break;
				}
				if(status[c] == 2 || status[c] == 3) {
					return content.substring(j, i+1);
				}
			}
		}
		return null;
		
	}
	
}
